<?php
require_once( ABSPATH . 'wp-load.php' );
require_once 'common-model.php';


class ProductModel{
    private $table = "ys_product";
    
    public static function insertNewProduct($data) {
        global $wpdb;
        try {
            $wpdb->insert( "ys_product", $data, $format);
            return $wpdb->insert_id;
        } catch (Exception $e) {
            //log here
            return false;
        }
    }

    public static function deleteProduct($condition) {
        global $wpdb;
        try {
            return $wpdb->delete($this->table, $condition, null);
        } catch (Exception $e) {
            //log here
            return false;
        }
    }

    public static function updateProduct($data, $where) {
        global $wpdb;
        try {
            return $wpdb->update("ys_product", $data, $where,$format,$where_format );
        } catch (Exception $e) {
            //log here
            return false;
        }
    }
    
    /**
     *  $wpdb->get_col( 'query', column_offset ); 
     *  $wpdb->get_results( 'query', output_type );
     *  $wpdb->get_row('query', output_type, row_offset);
     *  $wpdb->get_var( 'query', column_offset, row_offset );
     * 
     * @param $query : câu lệnh query
     * @param $data1 : get_row - output_type : OBJECT, ARRAY_A,ARRAY_N
     *                 get_col - column_offset 
     *                 get_generic -output_type : OBJECT, OBJECT_K, ARRAY_A,ARRAY_N
     *                 get_variable - column_offset 
     */
    public static function selectProduct($query, $data1 , $data2 = NULL, $selectType = CommonModel::SELECT_TYPE_RESULTS) {
        global $wpdb;
        try {
            if($selectType == CommonModel::SELECT_TYPE_COLUMN){
                return $wpdb->get_col($query, $data1);
            }
           
            if($selectType == CommonModel::SELECT_TYPE_RESULTS){
                return $wpdb->get_results($query, $data1);
            }
            if($selectType == CommonModel::SELECT_TYPE_VARIABLE){
                return $wpdb->get_var($query, $data1, $data2 );
            }
            
            if($selectType == CommonModel::SELECT_TYPE_ROW){
                return $wpdb->get_row($query, $data1, $data2);
            }
        } catch (Exception $e) {
            //log here
            return false;
        }
    }
}
?>
